package com.uniledger.exchange.contact;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.uniledger.exchange.R;
import com.uniledger.exchange.glide.GlideApp;

import java.util.ArrayList;
import java.util.List;

public class SearchAdapter extends RecyclerView.Adapter<ContactViewHolder> implements Filterable {

    List<UniContact> contacts;

    List<UniContact> contactList;

    Activity activity;
    LayoutInflater inflater;
    TextView noResult;

    public void setNoResult(TextView noResult) {
        this.noResult = noResult;
    }

    public SearchAdapter(Activity activity, List<UniContact> list) {
        this.activity = activity;
        this.contacts = new ArrayList<>();
        this.contactList = list;
        this.contacts.addAll(list);
        this.inflater = LayoutInflater.from(activity);
    }

    public UniContact getItemData(int pos) {
        return contacts.get(pos);
    }


    @NonNull
    @Override
    public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_uni_contact, parent, false);
        return new ContactViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ContactViewHolder contactViewHolder, int position) {
        UniContact entity = contacts.get(position);
        contactViewHolder.name.setText(entity.nick);
        contactViewHolder.mobile.setText(entity.mobile);
        GlideApp.with(activity)
                .load(entity.getAvatar())
                .error(R.drawable.head)
                .placeholder(R.drawable.head)
                .into(contactViewHolder.avatar);
    }

    @Override
    public int getItemCount() {
        return contacts.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                ArrayList<UniContact> list = new ArrayList<>();
                for (UniContact item : contactList) {
                    if (item.pinyin.startsWith(constraint.toString()) || item.nick.contains(constraint)) {
                        list.add(item);
                    }
                }
                FilterResults results = new FilterResults();
                results.count = list.size();
                results.values = list;
                return results;
            }

            @Override
            @SuppressWarnings("unchecked")
            protected void publishResults(CharSequence constraint, FilterResults results) {
                ArrayList<UniContact> list = (ArrayList<UniContact>) results.values;
                contacts.clear();
                contacts.addAll(list);
                if (results.count == 0) {
                    noResult.setVisibility(View.VISIBLE);
                } else {
                    noResult.setVisibility(View.INVISIBLE);
                }
                notifyDataSetChanged();
            }
        };
    }
}
